#!/bin/bash
#
# Target dist: Debian Stretch
DIST=stretch

rm -rf ${DIST}
cp -r debian ${DIST}

# No dnssec module:
sed -i -e '/libval-dev/d' -e '/^Package: kamailio-dnssec-modules/,/^$/d' \
  ${DIST}/control
sed -i -e '/^PACKAGE_GROUPS += dnssec/d' ${DIST}/rules
sed -i -e '/--EXCLUDED--/i EXTRA_EXCLUDED_MODULES += dnssec' ${DIST}/rules

# libbson not in libmongoc
sed -i 's/libmongoc-dev/libbson-dev,\n libmongoc-dev/' \
	${DIST}/control

# No secsipid module
sed -i -e '/libsecsipid-dev/d' -e '/^Package: kamailio-secsipid-modules/,/^$/d' \
	${DIST}/control
sed -i -e '/^PACKAGE_GROUPS += secsipid/d' ${DIST}/rules
sed -i -e '/--EXCLUDED--/i EXTRA_EXCLUDED_MODULES += secsipid' ${DIST}/rules

# libwebsockets >= 3.1
sed -i -e '/libwebsockets-dev/d' -e '/^Package: kamailio-lwsc-modules/,/^$/d' \
	${DIST}/control
sed -i -e '/^PACKAGE_GROUPS += lwsc/d' ${DIST}/rules
sed -i -e '/--EXCLUDED--/i EXTRA_EXCLUDED_MODULES += lwsc' ${DIST}/rules

# no libnats-dev
sed -i -e '/^ libnats-dev,/d' \
  -e '/^Package: kamailio-nats-modules/,/^$/d' \
  ${DIST}/control
sed -i -e '/^PACKAGE_GROUPS += nats/d' ${DIST}/rules
sed -i -e '/--EXCLUDED--/i EXTRA_EXCLUDED_MODULES += nats' ${DIST}/rules

# libmicrohttpd too old
sed -i -e '/^ libmicrohttpd-dev,/d' \
  -e '/^Package: kamailio-microhttpd-modules/,/^$/d' \
  ${DIST}/control
sed -i -e '/^PACKAGE_GROUPS += microhttpd/d' ${DIST}/rules
sed -i -e '/--EXCLUDED--/i EXTRA_EXCLUDED_MODULES += microhttpd' ${DIST}/rules

# libssl >= v1.1.1
sed -i -e '/^Package: kamailio-ims-modules/,/^$/d' \
  ${DIST}/control
sed -i -e '/^PACKAGE_GROUPS += ims/d' ${DIST}/rules
sed -i -e '/--EXCLUDED--/i EXTRA_EXCLUDED_MODULES += ims' ${DIST}/rules

# libwolfssl-dev < 5.2
sed -i -e '/^ libwolfssl-dev,/d' \
  -e '/^Package: kamailio-wolftls-modules/,/^$/d' \
  ${DIST}/control
sed -i -e '/^PACKAGE_GROUPS += tls_wolfssl/d' ${DIST}/rules
sed -i -e '/--EXCLUDED--/i EXTRA_EXCLUDED_MODULES += tls_wolfssl' ${DIST}/rules

# librdkafka-dev < 1.1.0
sed -i -e '/^ librdkafka-dev,/d' \
  -e '/^Package: kamailio-kafka-modules/,/^$/d' \
  ${DIST}/control
sed -i -e '/^PACKAGE_GROUPS += kafka/d' ${DIST}/rules
sed -i -e '/--EXCLUDED--/i EXTRA_EXCLUDED_MODULES += kafka' ${DIST}/rules

# libjwt < 1.12
sed -i -e '/libjwt-dev/d' -e 's/, JWT (JSON Web Token)//' ${DIST}/control
sed -i -e '/^EXTRA_GROUPS += jwt$/d' ${DIST}/rules

wrap-and-sort -sat -d ${DIST}

# clean backports scripts
rm -rf ${DIST}/backports
exit 0
